<?php

/* * 注意此文档为UTF-8的编码.
 * Created on 2010-6-16
 *
 * @author EXT <isrn@qq.com>
 *
 */
include ('../../includes/global.php');
include ('../global.php');
include ("../power.php");
require_once './render.php';
require_once './items_config.php';
$dbcfg = Sys::Gp(array('dbcfg', 'sevenel'));
Tr($_GET['lg'], 'lg');

/**
 * 初始化排序
 */
function pxcs() {
    $tid = $_POST['tid'];
    $_POST['lg'] != '' && $condition = " and lg like '" . $_POST['lg'] . "' ";
    if (!$tid)
        return;
    $nowfield = Dgd(array(
        'db' => 'd_sevenel',
        'cd' => "tid = $tid and isfld = 0 " . $condition . " order by px asc",
        'tn' => "fld"
            ));
    $px = 0;
    foreach ($nowfield as $value) {
        $gx['px'] = $px;
        Du('sevenel', 'fld', $gx, 'id', $value['id']);
        $px++;
    }
}

pxcs();

$donotrender = array('htmleditor', 'fckeditor', 'password', 'hidden');
switch ($_REQUEST['op']) {
    case 'form_component': {
            $id = $_GET['c_id'];

            if ($id != '0') {
                $field_data = Dgd(array(
                    'db' => 'd_sevenel',
                    'cd' => "id = $id",
                    'tn' => "fieldmod",
                    'limit' => '1'
                        ));
                $config = json_decode($field_data['config'], true);
            } else {
                $tree_id = $_GET['p_id'];
                Tr($tree_id, 'p_id');
                $tree = Dgd(array(
                    'db' => 'd_sevenel',
                    'cd' => "id = $tree_id",
                    'tn' => "tree",
                    'limit' => '1'
                        ));
                $tabname = gettabname($tree['table'], $dbcfg['prefix']);
                $field_ = Dgd(array(
                    'db' => 'd_sevenel',
                    'cd' => "parent_tree = $tree_id",
                    'tn' => "fieldmod",
                    'column' => 'field_name'
                        ));
                $fullfields = Dgd(array(
                    'db' => 'd_sevenel',
                    'sql' => "SHOW FULL FIELDS FROM " . $tabname
                        ));
                $doNotCreate = array('id', 'del', 'lg', 'gid', 'sortid', 'cataid');
                foreach ($field_ as $key => $value) {
                    $existfields[] = $value['field_name'];
                }
                foreach ($fullfields as $arrow => $data) {
                    if (!in_array($data['Field'], $existfields) && !in_array($data['Field'], $doNotCreate)) {
                        $store_[] = "['{$data['Field']}']";
                    }
                }
                if ($store_ == '') {
                    exit('没有可用的字段');
                }
                foreach ($store_ as $array => $f) {
                    $store .= $f;
                    if ($array < count($store_) - 1) {
                        $store .= ',';
                    }
                }
            }
            switch ($_REQUEST['component']) {
                case 'combo': {
                        Tr($id, 'id');
                        Tr($store, 'store');
                        Tr($config['pair'], 'data');
                        Ts('form/combo.html');
                        break;
                    }
                case 'checkboxgroup': {
                        $config_num = count($config);
                        if ($config_num != 0) {
                            foreach ($config as $key => $data) {
                                if ($data['checked'] == '1') {
                                    $checked = "checked: true,";
                                }else
                                    $checked = "";
                                $items.= "{xtype:'checkbox',
								boxLabel: '{$data['name']}',
								name: 'checkbox',
								columnWidth:.25,
								$checked
								listeners:{
										'check':function(checkbox){
												if(checkbox.checked){
												var label = checkbox.boxLabel;
												var value = checkbox.inputValue;
												Ext.getCmp('label').setValue(label);
												Ext.getCmp('value').setValue(value);
												}
										}},
								inputValue:'{$data['value']}'
								}";
                                if ($key < $config_num - 1) {
                                    $items.= ",";
                                }
                            }

                            $items = ",items:[{$items}]";
                        } else {
                            Tr($store, 'store');
                        }
                        Tr($id, 'id');
                        Tr($items, 'items');
                        Ts('form/checkboxgroup.html');
                        break;
                    }
                case 'radiogroup': {
                        $config_num = count($config);
                        if ($config_num != 0) {
                            foreach ($config as $key => $data) {
                                if ($data['checked'] == '1') {
                                    $checked = "checked: true,";
                                }else
                                    $checked = "";
                                $items.= "{xtype:'radio',
								boxLabel: '{$data['name']}',
								name: 'radio',
								columnWidth:.25,
								$checked
								listeners:{
										'check':function(radio){
												if(radio.checked){
												var label = radio.boxLabel;
												var value = radio.inputValue;
												Ext.getCmp('label').setValue(label);
												Ext.getCmp('value').setValue(value);
												}
										}},
								inputValue:'{$data['value']}'
								}";
                                if ($key < $config_num - 1) {
                                    $items.= ",";
                                }
                            }
                            $items = ",items:[{$items}]";
                        } else {
                            Tr($store, 'store');
                        }
                        Tr($id, 'id');
                        Tr($items, 'items');
                        Ts('form/radiogroup.html');
                        break;
                    }
            }
            break;
        }
    case 'form_component_del': {
            $_POST['ids'] = json_decode(stripslashes($_POST['ids']));
            if (is_array($_POST['ids'])) {
                foreach ($_POST['ids'] as $id) {
                    //Dd('sevenel', 'fld', 'id', $id, 1, 1);
                    $gx[$_POST['field']] = $_POST['val'];
                    Du('sevenel', 'fld', $gx, 'id', $id);
                }
            } else {
                //Dd('sevenel', 'fld', 'id', $_POST['ids'], 1, 1);
                $gx[$_POST['field']] = $_POST['val'];
                Du('sevenel', 'fld', $gx, 'id', $_POST['ids']);
            }
            echo 'ok';
            break;
        }
    case 'form_component_save': {
            switch ($_REQUEST['component']) {
                case 'combo': {
                        $id = $_POST['id'];
                        $param = explode('-', $_POST['param']);
                        foreach ($param as $key => $value) {
                            if ($value != null) {
                                $items = explode(',', $value);
                                $str = "'{$items['0']}','{$items['1']}'";
                                $str = '[' . $str . ']';
                                $combo .= $str;
                                if ($key < count($param) - 2) {
                                    $combo .= ',';
                                }
                            }
                        }
                        $combo = addslashes($combo);
                        if ($id != 0) {
                            $save = array('config' => '{"pair":"' . $combo . '"}');
                            Du('sevenel', 'fieldmod', $save, 'id', $id, 1);
                        } else {
                            $not = '0';
                            $type = $_REQUEST['component'];
                            if (in_array($type, $donotrender)) {
                                $not = '1';
                            }
                            $field = $_POST['field'];
                            $pid = $_POST['p_id'];
                            $type = $_REQUEST['component'];
                            $save = array('field_name' => $field, 'config' => '{"pair":"' . $combo . '"}', 'parent_tree' => $pid, 'type' => $type, 'donotrender' => $not);
                            Di('sevenel', 'fieldmod', $save);
                        }
                        break;
                    }
                case 'checkboxgroup': {
                        
                    }
                case 'radiogroup': {
                        $id = $_POST['id'];
                        $param = explode('-', $_POST['param']);
                        foreach ($param as $key => $value) {
                            if ($value != null) {
                                $check = null;
                                $items = explode(',', $value);
                                if ($items['2'] != null && $items['2'] == 1) {
                                    $check = ',"checked":"1"';
                                }
                                $str = '{"name":"' . $items['0'] . '","value":"' . $items['1'] . '"' . $check . '}';

                                $combo .= $str;
                                if ($key < count($param) - 2) {
                                    $combo .= ',';
                                }
                            }
                        }
                        $combo = '[' . $combo . ']';
                        if ($id != 0) {
                            $save = array('config' => $combo);
                            Du('sevenel', 'fieldmod', $save, 'id', $id, 1);
                        } else {
                            $not = '0';
                            $type = $_REQUEST['component'];
                            if (in_array($type, $donotrender)) {
                                $not = '1';
                            }
                            $field = $_POST['field'];
                            $pid = $_POST['p_id'];
                            $type = $_REQUEST['component'];
                            $save = array('field_name' => $field, 'config' => $combo, 'parent_tree' => $pid, 'type' => $type, 'donotrender' => $not);
                            Di('sevenel', 'fieldmod', $save);
                        }
                        break;
                    }
            }
            break;
        }
    case 'combo_store': {
            //是否为新增字段
            autoAddField('se_fld', 'isnew');
            foreach ($type_array as $key => $value) {
                $combo[] = array('name' => $value['name'], 'value' => $value['value']);
            }
            echo json_encode($combo);
            break;
        }
    case 'formitem': {
            $tid = $_POST['tid'];
            $_POST['lg'] != '' && $condition = " and lg like '" . $_POST['lg'] . "' ";
            $field_data = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "tid = $tid and isfld = 0 " . $condition . " order  by px asc",
                //'ce'=>1,
                'tn' => "fld"
                    ));

            if ($field_data[count($field_data) - 1]['px'] < 2) {
                $px = 0;
                foreach ($field_data as $value) {
                    $gx['px'] = $px;
                    Du('sevenel', 'fld', $gx, 'id', $value['id']);
                    $px++;
                }
            }

            if ($field_data != '') {
                foreach ($field_data as $key => $value) {
                    $needconfig = 0;
                    if ($value['cfg'] != '') {
                        $needconfig = 1;
                    } else {
                        foreach ($type_array as $arrow => $data) {
                            if ($value['type'] == $data['name'] && $data['param'] == '0') {
                                $needconfig = 1;
                            }
                        }
                    }
                    $donotrender = $value['xr'] == '0' ? 0 : 1;
                    $field[] = array(
                        'id' => $value['id'],
                        'fname' => $value['fname'],
                        'name' => $value['name'],
                        'type' => $value['type'],
                        'px' => $value['px'],
                        'cfg' => $needconfig,
                        'xr' => $donotrender,
                        'isnew' => $value['isnew'],
                        'del' => $value['del']
                    );
                }
            }

            foreach ($field_data as $value) {
                $gx['isnew'] = 0;
                Du('sevenel', 'fld', $gx, 'id', $value['id']);
                $px++;
            }
            echo json_encode($field);
            break;
        }
    case 'items_config_save_ingrid': {
            $field = $_POST['field'];
            $checkbox = array('xr', 'del');
            if (in_array($field, $checkbox)) {
                $value = $_POST['value'] == 'true' ? '1' : '0';
            } else {
                $value = $_POST['value'];
            }
            $id = $_POST['id'];
            $save = array($field => $value);
            Du('sevenel', 'fld', $save, 'id', $id, 1);
            if ($field == 'type' && !in_array($field, $checkbox)) {
                $save = array('cfg' => '');
                Du('sevenel', 'fld', $save, 'id', $id, 1);
            }
            ext_request('保存成功[success]');
            break;
        }
    case 'items_config_add': {
            $not = '0';
            $type = $_POST['type'];
            if (in_array($type, $donotrender)) {
                $not = '1';
            }
            $pid = $_POST['pid'];
            $field = $_POST['field'];
            $param = explode(',', $_POST['param']);
            foreach ($param as $v) {
                $save[$v] = $_POST[$v];
            }
            $save = json_encode($save);
            $save = array('field_name' => $field, 'config' => $save, 'parent_tree' => $pid, 'type' => $type, 'donotrender' => $not);
            Di('sevenel', 'fieldmod', $save);
            ext_request('ok');
            break;
        }
    case 'items_config_save': {
            $param = explode(',', $_POST['param']);
            foreach ($param as $v) {
                $save[$v] = $_POST[$v];
            }
            $save = json_encode($save);
            $save = array('config' => $save);
            Du('sevenel', 'fld', $save, 'id', $_POST['id'], 1);
            ext_request('ok');
            break;
        }
    case 'px': {
            $_POST['lg'] != '' && $condition = " and lg like '" . $_POST['lg'] . "' ";

            $id = $_POST['nowid'];
            $nowfield = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "id = $id ",
                'tn' => "fld",
                'limit' => 1
                    ));
            $gx['px'] = $_POST['wz'];   //上上下下
            //$gx['px'] = $_POST['wz']>$nowfield['px']?$_POST['wz']-1:$_POST['wz']; //这个是只在上面
            $oldfld = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "tid = '" . $nowfield['tid'] . "' and px='" . $_POST['wz'] . "' " . $condition . "",
                'tn' => "fld",
                'limit' => 1
                    ));
            $xx['px'] = $nowfield['px'];
//			Du('sevenel','fld',$gx,'id',$id);
            if ($xx['px'] > $_POST['wz']) {
                $d_sevenel->Desql("update  `se_fld` set px=px+1 WHERE 1 and tid = '" . $nowfield['tid'] . "' and px>='" . $_POST['wz'] . "' and px <='" . $xx['px'] . "' " . $condition . "");
                Du('sevenel', 'fld', $gx, 'id', $id);
            } else {
                $d_sevenel->Desql("update  `se_fld` set px=px-1 WHERE 1 and tid = '" . $nowfield['tid'] . "' and px>='" . $xx['px'] . "' and px <='" . $_POST['wz'] . "' " . $condition . " ");
                Du('sevenel', 'fld', $gx, 'id', $id);
            }
            ext_request('ok');
        }
    case 'pxcs': {
            pxcs();
            ext_request('ok');
            break;
        }
    case 'cs': {
            $tid = $_POST['tid'];
            $tree = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "id = $tid",
                'tn' => "tree",
                'limit' => '1'
                    ));
            $_POST['lg'] != '' && $condition = " and lg like '" . $_POST['lg'] . "' ";
            $bid = $tree['table'];
            $pingzd = array('id', 'del', 'tid', 'gid', 'at', 'lg', 'bdtid', 'bdid');
            $nowfield = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "tid = $tid and isfld = 0 " . $condition . " order by px+0 desc ",
                'tn' => "fld"
                    ));
            foreach ($nowfield as $value) {
                $pingzd[] = $value['fname'];
            }
            $tabname = gettabname($bid, $dbcfg['prefix']);
            $fullfields = Dgd(array(
                'db' => 'd_sevenel',
                'sql' => "SHOW FULL FIELDS FROM " . $tabname
                    ));
            /**
             * 最大排序号
             * 功能：用此字段将新字段都放到最后
             */
            $max_px = 0;
            foreach ($fullfields as $key => $value) {
                if (!in_array($value['Field'], $pingzd)) {
                    $tempcfg = Dgd(array(
                        'db' => 'd_sevenel',
                        'cd' => "tid={$tid} and fname = '{$value['Field']}' order by id desc ",
                        'tn' => "fld",
                        'limit' => 1
                            ));
                    if ($tempcfg['id'] == '') {
                        $tempcfg = Dgd(array(
                            'db' => 'd_sevenel',
                            'cd' => " fname = '{$value['Field']}' order by id desc ",
                            'tn' => "fld",
                            'limit' => 1
                                ));
                    }

                    $max_px = $tempcfg['px'] > $max_px ? $tempcfg['px'] : $max_px;

                    if ($tempcfg) {
                        $tempcfg['id'] = '';
                        $tempcfg['tid'] = $tid;
                        if ($_POST['lg'] != '') {
                            $tempcfg['lg'] = $_POST['lg'];
                        } else {
                            $tempcfg['lg'] = 'zh';
                        }

                        Di('sevenel', 'fld', $tempcfg);
                    } else {
                        $new_px[] = array('Field' => $value['Field'], 'Comment' => $value['Comment']);
                    }
                }
            }
            foreach ($new_px as $v) {
                $max_px++;
                $tempcfg['id'] = '';
                $tempcfg['tid'] = $tid;
                $tempcfg['fname'] = $v['Field'];
                $tempcfg['name'] = $v['Comment'];
                $tempcfg['type'] = 'textfield';
                $tempcfg['isnew'] = '1';
                $tempcfg['px'] = $max_px;
                if ($_POST['lg'] != '') {
                    $tempcfg['lg'] = $_POST['lg'];
                } else {
                    $tempcfg['lg'] = 'zh';
                }

                Di('sevenel', 'fld', $tempcfg);
            }
            ext_request('ok');
            break;
        }
    case 'qk': {
            $_POST['lg'] != '' && $condition = " and lg like '" . $_POST['lg'] . "' ";
            $tempz = $d_sevenel->Dqsql("DELETE FROM `se_fld` WHERE tid like '" . $_POST['tid'] . "' " . $condition . " ;");
            ext_request('ok');
        }
    default: {
            if (strpos($_GET['tid'], '-') !== false) {
                $tid = explode('-', $_GET['tid']);
                $tid = $tid[1];
            } else {
                $tid = $_GET['tid'];
            }
            Tr($tid, 'tid');

            /**
             * 自动生成del字段
             */
            autoAddField('se_fld', 'del');

            $tree = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "id = $tid ",
                'tn' => "tree",
                'limit' => '1'
                    ));
            if ($_GET['lg'] == '' && $tree['config'] != '') {
                $tconfig = json_decode($tree['config'], true);
                $nowlg = explode('|', $tconfig['lg']);
                foreach ($nowlg as $tv) {
                    $standlang[$tv] && $xhlg[] = array('lg' => $tv, 'value' => $standlang[$tv]);
                }
                if ($xhlg) {
                    Tr($xhlg, 'dy');
                    //print_r($xhlg);
                    Ts('form/index.html');
                    exit;
                }
            }
            $bid = $tree['table'];
            if ($bid) {
                $tabname = gettabname($tree['table'], $dbcfg['prefix']);

                Tr($tid, 'tid');
                Tr($tree['title'], 'tname');
                Tr($tabname, 'sytable');
                Ts('form/form_config.html');
            } else {
                exit("该模型还未使用表");
            }
        }
}
?>
